package piis;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Map;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RefineryUtilities;

/**
 * This class is used to draw a chart.
 *
 * @author Atuan
 */
public class Plot  extends JFrame {
    private static final long serialVersionUID = 1L;

    public Plot(String title, String seriesName, Map<Double,Double> data, double markerValue) {
        super(title);
        final XYSeriesCollection dataset = createDataset(seriesName, data);
        final JFreeChart chart = ChartFactory.createXYLineChart(
            seriesName,
            "X", 
            "Y", 
            dataset,
            PlotOrientation.VERTICAL,
            false,
            true,
            false
        );
        // Prepare and adding marker for highlighting the bestX value.
        Marker marker = new ValueMarker(markerValue);
        marker.setStroke(new BasicStroke(1));
        marker.setPaint(Color.GREEN);
        chart.getXYPlot().addDomainMarker(marker);
        
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(1000, 570));
        setContentPane(chartPanel);
    }
    public Plot(String title, String seriesName, Map<Double,Double> data, double[][] pointcloud, double[][] trainingSet) {
        super(title);
        final XYSeriesCollection dataset = createDataset(seriesName, data, pointcloud, trainingSet);
        final JFreeChart chart = ChartFactory.createXYLineChart(
            seriesName,
            "X", 
            "Y", 
            dataset,
            PlotOrientation.VERTICAL,
            false,
            true,
            false
        );
        XYPlot plot = (XYPlot) chart.getPlot();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(1, true);
        renderer.setSeriesShapesVisible(1, false);
        renderer.setSeriesLinesVisible(0, false);
        renderer.setSeriesShapesVisible(0, true); 
        plot.setRenderer(renderer);
        
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(1000, 570));
        setContentPane(chartPanel);
    }
    
    

    
    
    /**
     * This method prepares a dataset from data, that suitable for a Chart. 
     * @param seriesName : title of the series.
     * @param data : Map of data (x,y).
     * @return 
     */
    private XYSeriesCollection createDataset(String seriesName, Map<Double,Double> data) { 
        // create the dataset...
        final XYSeries series = new XYSeries(seriesName);
        for(Map.Entry<Double,Double> entry : data.entrySet()){
            series.add(entry.getKey(),entry.getValue());
        }
        
        XYSeriesCollection dataset = new XYSeriesCollection(series);
        
        return dataset;
                
    }
    
    private XYSeriesCollection createDataset(String seriesName, Map<Double,Double> data, double[][] pointcloud, double[][] trainingSet) { 
        // create the dataset...
        final XYSeries series1 = new XYSeries("PointCloud");
        final XYSeries series2 = new XYSeries(seriesName);
        for(Map.Entry<Double,Double> entry : data.entrySet()){
            series2.add(entry.getKey(),entry.getValue());
        }
        for (int i = 0; i < trainingSet.length; i++) {
            for (int j = 0; j < trainingSet[i].length; j++) {
                series1.add(trainingSet[i][j], pointcloud[i][j]);
            }
        }
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series1);
        dataset.addSeries(series2);
        
        return dataset;
                
    }
    
   
}
